/*
 * Group.java
 *
 * Created on 14 Май 2008 г., 21:35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jimm.protocol;

import java.util.Vector;

import org.jimm.app.ContactListActivity;
import org.jimm.app.JimmApplication;
import org.jimm.comm.Util;
import org.jimm.service.Preferences;
//import org.jimm.forms.ManageContactListForm;

import android.view.ContextMenu;
import android.view.View;

/**
 * @author vladimir
 */
public class Group extends TreeBranch implements TreeNode {
    private String name;
    private View convertView;
    private final Vector<Contact> contacts = new Vector<Contact>();
    private final Vector<Contact> sortedContacts = new Vector<Contact>();
    private Preferences p = JimmApplication.getInstance().getPreferences();

    /**
     * Creates a new instance of Group
     */
    public Group(String name) {
        setName(name);
        caption = name;
        setMode(Group.MODE_FULL_ACCESS);
    }

    public void setConvertView(View convertView) {
        this.convertView = convertView;

    }

    public View getConvertView() {
        return convertView;
    }

    // Returns the group item name
    public final String getName() {
        return this.name;
    }

    // Sets the group item name
    public final void setName(String name) {
        this.name = name;
    }

    public static final int NOT_IN_GROUP = -1;

    public static final byte MODE_NONE = 0x00;
    public static final byte MODE_REMOVABLE = 0x01;
    public static final byte MODE_EDITABLE = 0x02;
    public static final byte MODE_NEW_CONTACTS = 0x04;
    public static final byte MODE_FULL_ACCESS = 0x0F;

    public static final byte MODE_TOP = 0x10;
    public static final byte MODE_BOTTOM = 0x20;
    public static final byte MODE_BOTTOM2 = 0x40;

    private byte mode;

    public final void setMode(int newMode) {
        mode = (byte) newMode;
    }

    public final boolean hasMode(byte type) {
        return (mode & type) != 0;
    }

    public int getNodeWeight() {
        if (hasMode(MODE_TOP)) return -4;
        if (hasMode(MODE_BOTTOM)) return -2;
        if (hasMode(MODE_BOTTOM2)) return -1;
        //if (!hasMode(MODE_EDITABLE)) return -2;
        //if (!hasMode(MODE_REMOVABLE)) return -1;
        return -3;
    }

    private int groupId;

    public final int getId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    private String caption = null;

    public final String getText() {
        return caption;
    }

    public final boolean hasUnreadMessage() {
        int size = contacts.size();
        for (int i = 0; i < size; ++i) {
            if (contacts.elementAt(i).getUnreadMessageCount() > 0) {
                return true;
            }
        }
        return false;
    }

    public boolean isEmpty() {
        return (0 == contacts.size());
    }

    final void addContact(Contact c) {
        contacts.addElement(c);
    }

    final boolean removeContact(Contact c) {
        return contacts.removeElement(c);
    }

    public final Vector<Contact> getContacts() {
        return contacts;
    }

    public final Vector<Contact> getSortedContacts() {
        return sortedContacts;
    }

    public final int getOnlineCount() {
        int onlineCount = 0;
        int total = contacts.size();
        for (int i = 0; i < total; ++i) {
            Contact item = contacts.elementAt(i);
            if (item.isVisibleInContactList()) {
                onlineCount++;
            }
        }
        return onlineCount;
    }

    // Calculates online/total values for group
    public final void updateGroupData() {
        int onlineCount = 0;
        int total = contacts.size();
        for (int i = 0; i < total; ++i) {
            Contact item = contacts.elementAt(i);
            if (item.isOnline()) {
                onlineCount++;
            }
        }
        caption = getName();
        if (0 < total) {
            caption += " (" + onlineCount + "/" + total + ")";
        }
    }

    public final void sortContacts() {
        sortedContacts.clear();
        Contact c;
        for (int contactIndex = 0; contactIndex < contacts.size(); ++contactIndex) {
            c = (Contact) contacts.elementAt(contactIndex);
            if (!p.hideOfflineContacts || c.isVisibleInContactList()) {
                sortedContacts.addElement(c);
            }
        }
    }

    public final void sort() {
        if (isExpanded()) {
            Util.sort(contacts);
            sortContacts();
        }
    }

    public final ContextMenu getContextMenu(ContextMenu menu, ContactListActivity a) {
        //if (p.isConnected()) {
        //return new ManageContactListForm(p, this).getMenu();
        //}
        return null;
    }
}
